package com.yndsoft.jc.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yndsoft.jc.R;
import com.yndsoft.jc.ui.xiaoyao.XYProgressDialog;

public class DialogHelper {
    //Toast提示
    public static void showLongToast(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }

    //Toast提示
    public static void showLongToast(Context context, int msgId) {
        Toast.makeText(context, msgId, Toast.LENGTH_LONG).show();
    }

    //Toast提示
    public static void showShortToast(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    //Toast提示
    public static void showShortToast(Context context, int msgId) {
        Toast.makeText(context, msgId, Toast.LENGTH_SHORT).show();
    }

    //弹出框
    public static Dialog createConfirmDialog(Context context, int iconId, String title, String message, String positiveBtnName, String negativeBtnName, android.content.DialogInterface.OnClickListener positiveBtnListener, android.content.DialogInterface.OnClickListener negativeBtnListener) {
        Dialog dialog = null;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(iconId);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(positiveBtnName, positiveBtnListener);
        builder.setNegativeButton(negativeBtnName, negativeBtnListener);
        dialog = builder.create();
        return dialog;
    }

    //得到自定义的progressDialog
    public static Dialog createLoadingDialog(Activity activity) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        View v = inflater.inflate(R.layout.layout_loading_dialog, null);        // 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);  // 加载布局
        ImageView spaceshipImage = (ImageView) v.findViewById(R.id.layout_loading_dialog_img);
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(activity, R.anim.loading_animation); // 加载动画
        spaceshipImage.startAnimation(hyperspaceJumpAnimation);             // 使用ImageView显示动画
        Dialog loadingDialog = new Dialog(activity, R.style.layout_loading_dialog); // 创建自定义样式dialog
        loadingDialog.setCanceledOnTouchOutside(true);//设置点击Dialog外部任意区域关闭Dialog
        //loadingDialog.setCancelable(false);// 不可以用"返回键"取消
        //原写法
        //loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

        //新的控制大小写法
        //loadingDialog.setContentView(layout);
        //Window dialogWindow = loadingDialog.getWindow();
        //WindowManager m = activity.getWindowManager();
        //Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        //WindowManager.LayoutParams p = loadingDialog.getWindow().getAttributes();  //获取对话框当前的参数值
        //p.height = (int) d.getHeight();
        //p.width = (int) d.getWidth();
        //loadingDialog.getWindow().setAttributes(p);     //设置生效
        //WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        //p.x = -80;//设置x坐标
        //p.y = -60;//设置y坐标
        //p.width = (int) (d.getWidth() * 0.5); // 宽度设置为屏幕的0.5
        //p.height = (int) (d.getHeight() * 0.2); // 高度设置为屏幕的0.2
        //p.width = (int) (d.getWidth());
        //p.height = (int) (d.getHeight());
        //dialogWindow.setAttributes(p);
        return loadingDialog;
    }

    //得到自定义的progressDialog
    public static Dialog createLoadingDialogByMsg(Activity activity, String msg) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        View v = inflater.inflate(R.layout.layout_loading_dialog_msg, null);        // 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.layout_loading_dialog_msg);  // 加载布局
        ImageView spaceshipImage = (ImageView) v.findViewById(R.id.layout_loading_dialog_msg_img);
        TextView spaceshipText = (TextView) v.findViewById(R.id.layout_loading_dialog_msg_txt);
        spaceshipText.setText(msg);
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(activity, R.anim.loading_animation); // 加载动画
        spaceshipImage.startAnimation(hyperspaceJumpAnimation);             // 使用ImageView显示动画
        Dialog loadingDialog = new Dialog(activity, R.style.layout_loading_dialog); // 创建自定义样式dialog
        loadingDialog.setCanceledOnTouchOutside(true);//设置点击Dialog外部任意区域关闭Dialog
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        return loadingDialog;
    }

    //显示进度条
    public void showmProgressDialogCustom(Context context, String msg) {
        XYProgressDialog dialog = new XYProgressDialog(context, msg);
        dialog.show();
    }

    //关闭进度条
    public void closeProgressDialogCustom(Context context, String msg) {
        XYProgressDialog dialog = new XYProgressDialog(context, msg);
        dialog.cancel();
    }
}